package com.lingyun.ucm.web.mvc.controller;

import com.lingyun.ucm.common.exception.ControllerException;
import com.lingyun.ucm.common.pojo.bean.BusinessSystem;
import com.lingyun.ucm.common.pojo.bean.ConfigStoreValue;
import com.lingyun.ucm.common.util.Constant;
import com.lingyun.ucm.web.mvc.service.ConfigCenterService;
import com.lingyun.ucm.web.mvc.service.impl.ConfigCenterServiceImpl;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * 
 * @author Terrence2013
 * 2018年1月24日 上午9:50:54
 * TODO
 */
public class Configs4DetailServlet extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = -8309546829340938747L;

    private ConfigCenterService configCenterService = new ConfigCenterServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=utf-8");
            //分发请求
            this.distributeOperation(req);
            req.getRequestDispatcher("/WEB-INF/pages/configs4detail.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            //TODO 异常处理
        }
    }

    /**
     * 分发请求
     * <p>
     * path需要带"/"
     *
     * @param req
     */
    private void distributeOperation(HttpServletRequest req) throws ControllerException {
        try {
            String operation = req.getParameter("operation");
            String systemName = req.getParameter("systemName");
            String moduleName = req.getParameter("moduleName");
            String configKey = req.getParameter("configKey");
            String configValue = req.getParameter("configValue");
            if (operation != null) {
                switch (operation) {
                    case "save":
                        boolean canEdit = !"0".equals(req.getParameter("canEdit"));
                        boolean canDel = !"0".equals(req.getParameter("canDel"));
                        configCenterService.save(systemName, moduleName, configKey, new ConfigStoreValue(canEdit, canDel, configValue));
                        break;
                    case "update":
                        String oldConfigKey = req.getParameter("oldConfigKey");
                        configCenterService.update(systemName, moduleName, configKey, configValue, oldConfigKey);
                        break;
                    case "delete":
                        configCenterService.delete(systemName, moduleName, configKey);
                        break;
                    case "deleteModule":
                        configCenterService.deleteModule(systemName, moduleName);
                        break;
                    case "saveModule":
                        configCenterService.saveModule(systemName, moduleName);
                        break;
                    default:
                }
            }
            //获取列表数据
            String path = systemName == null ? Constant.ZK_ROOT_PATH : Constant.ZK_ROOT_PATH + "/" + systemName;
            List<BusinessSystem> businessSystemsList = configCenterService.list(path);
            req.setAttribute("businessSystemsList", businessSystemsList);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }
}
